local registry = require 'bee.registry'
local fs = require 'bee.filesystem'
local uni = require 'bee.unicode'
local CONFIG_PATH = '.vscode\\War3Config.json'

local config
if IO.Exists(CONFIG_PATH) then
    config = json.decode(IO.Read(CONFIG_PATH))
else
    config = {}
end

local Types = arg[2]
local Value = arg[3]

config['War3Registry'] = config['War3Registry'] or {}

registry.open [[HKEY_CURRENT_USER\SOFTWARE\Blizzard Entertainment]][""] = ""
registry.open [[HKEY_CURRENT_USER\SOFTWARE\Blizzard Entertainment\Warcraft III]]["Allow Local Files"] = 1

if config['War3Registry']['GamePath'] ~= nil then
    local commands = registry.open [[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Blizzard Entertainment\Warcraft III]]

    if commands then
        if Types == "War3ExePath" and Value ~= nil then
            registry.open [[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Blizzard Entertainment]][""] = ""
            registry.open [[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Blizzard Entertainment\Warcraft III]][""] = ""
            commands['GamePath'] = Value
            commands['InstallPath'] = fs.path(Value):parent_path():string():gsub('/', '\\') .. '\\'
        end
        config['War3Registry']['GamePath'] = commands['GamePath'] or "不存在"
        config['War3Registry']['InstallPath'] = commands['InstallPath'] or ""
    else
        config['War3Registry']['GamePath'] = "不存在"
        config['War3Registry']['InstallPath'] = ""
    end
end


if Types == "LocalUserName" then
    registry.open [[HKEY_CURRENT_USER\Software\Blizzard Entertainment\Warcraft III\String]]["userlocal"] = Value
    registry.open [[HKEY_CURRENT_USER\Software\Blizzard Entertainment\WorldEdit]]['Test Map - Player Profile'] = uni.a2u(
        Value)
end

local WorldEdit = registry.open [[HKEY_CURRENT_USER\Software\Blizzard Entertainment\WorldEdit]]
if WorldEdit then
    if WorldEdit['Test Map - Player Profile'] then
        config['LocalUserName'] = uni.u2a(
            WorldEdit['Test Map - Player Profile'])
    end
end

IO.Write(CONFIG_PATH, json.encode(config))

print("✅ 更新配置:War3Config.json")
